/**
 * 
 */
package com.servicecloud.config;

import java.util.Arrays;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * zuul跨域配置,这只是一种解决跨域的方案，还有比如通过nginx来解决
 *
 * @author Gavin<br>
 *         2020年4月19日
 */
@Configuration
public class CrosConfig {
	@Bean
	public CorsFilter corsFilter() {
		final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
		final CorsConfiguration config = new CorsConfiguration();
		config.setAllowCredentials(true);
		config.setAllowedOrigins(Arrays.asList("*"));
		config.setAllowedHeaders(Arrays.asList("*"));
		config.setAllowedMethods(Arrays.asList("*"));
		config.setMaxAge(300l);// 在多长时间请求内就不会对该请求进行跨域检查

		source.registerCorsConfiguration("/**", config);// https://www.a.com...

		return new CorsFilter(source);
	}
}
